package com.antaohua.tools.api.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.antaohua.tools.api.call.AliyunCall;
import com.antaohua.tools.api.domain.APIResultDomain;
import com.antaohua.tools.api.entity.InterfacePathEntity;

import com.antaohua.tools.api.entity.InterfaceProviderKeyEntity;
import com.antaohua.tools.api.enums.CallMethod;
import com.antaohua.tools.api.factory.CallFactory;
import com.antaohua.tools.api.interfaces.CallApiInterface;
import com.antaohua.tools.api.service.InterfaceCallService;
import com.antaohua.tools.api.service.InterfacePathService;
import com.antaohua.tools.api.service.InterfaceProviderKeyService;
import com.antaohua.tools.framework.base.exception.QureyEntityException;

@Service
public class InterfaceCallServiceImpl implements InterfaceCallService{

	@Autowired
	private InterfacePathService pathService;
	
//	@Autowired
//	private InterfaceProviderKeyService providerKeyService;
	
	@Autowired
	private InterfaceProviderKeyService keyService;
	
	@Autowired
	private CallFactory callFactory;
	
	@Override
	public APIResultDomain get(String interfaceCode,Map<String, String> parameter) throws ClientProtocolException, IOException, QureyEntityException {
		List<InterfacePathEntity> listPath = pathService.findByProperty("code",interfaceCode);
		if(listPath==null||listPath.size()<1){
			throw new QureyEntityException("未查询到查询接口");
		}
		InterfacePathEntity path =   listPath.get(0);
		
		
		
		Map<String, String> key = null;
		
		if(path.getIsValidate()){
			List<InterfaceProviderKeyEntity> keys = keyService.findByProperty("provider",path.getProvider());
			key = new HashMap<>();
			for (InterfaceProviderKeyEntity interfaceProviderKeyEntity : keys) {
				key.put(interfaceProviderKeyEntity.getKeyName(), interfaceProviderKeyEntity.getKeyValue());
			}
		}
		CallApiInterface call = callFactory.getCall(path.getProvider().getCode());
		String resultString = call.call(path.getHost(), path.getUrl(), key, parameter,CallMethod.get);
		
		APIResultDomain result = new APIResultDomain();
		result.setData(resultString);
		result.setInterfaceCode(interfaceCode);
		result.setParameter(parameter);
		return result;
	}

	@Override
	public APIResultDomain post(String interfaceCode,Map<String, String> parameter) throws ClientProtocolException, IOException, QureyEntityException {
		List<InterfacePathEntity> listPath = pathService.findByProperty("code",interfaceCode);
		if(listPath==null||listPath.size()<1){
			throw new QureyEntityException("未查询到查询接口");
		}
		InterfacePathEntity path =   listPath.get(0);
		
		Map<String, String> key = null;
		
		if(path.getIsValidate()){
			List<InterfaceProviderKeyEntity> keys = keyService.findByProperty("provider",path.getProvider());
			key = new HashMap<>();
			for (InterfaceProviderKeyEntity interfaceProviderKeyEntity : keys) {
				key.put(interfaceProviderKeyEntity.getKeyName(), interfaceProviderKeyEntity.getKeyValue());
			}
		}
		CallApiInterface call = callFactory.getCall("aliyun");
		String resultString = call.call(path.getHost(), path.getUrl(), key, parameter,CallMethod.post);
		
		APIResultDomain result = new APIResultDomain();
		result.setData(resultString);
		result.setInterfaceCode(interfaceCode);
		result.setParameter(parameter);
		return result;
	}

}
